<?php

abstract class BaseDao {
    /**
     * 基础数据访问操作类
     * @author liuhui 2009-6-12
     *
     */
    private $connection = null;

    # 获取数据库链接
    protected function getConnection () {
        if ($this->connection) {
            return $this->connection;
        }
        require_once 'modules/dao/connection.php';
        $this->connection = Connection::getInstance()->getConnection();
        return $this->connection;
    }

    # 增加一条记录
    protected function _insert (array $fields, $table) {
        $sql = sprintf("INSERT INTO %s %s ", $table, $this->_buildInsertClause($fields));
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        return $stmt->rowCount();
    }

    # 更新一条记录
    protected function _update (array $fields, $table, $wheres = '', $sqli = '') {
        $sql = sprintf("UPDATE %s %s %s %s", $table, $this->_buildUpdateClause($fields), $this->_buildWhereClause($wheres), $sqli);
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        return $stmt->rowCount();
    }

    # 获取一条记录
    protected function _get ($table, $wheres = '', $sqli = '') {
        $sql = sprintf("SELECT * FROM %s %s %s ", $table, $this->_buildWhereClause($wheres), $sqli);
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetch();
        return ($result) ? $result : null;
    }

    # 获取多条记录
    protected function _gets ($table, $wheres = '', $sqli = '') {
        $sql = sprintf("SELECT * FROM %s %s %s ", $table, $this->_buildWhereClause($wheres), $sqli);
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll();
        return ($result) ? $result : null;
    }

    # 统计记录数
    protected function _count ($table, $wheres = '', $sqli = '') {
        $sql = sprintf("SELECT COUNT(*) FROM %s %s %s ", $table, $this->_buildWhereClause($wheres), $sqli);
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchColumn();
        return ($result) ? $result : 0;
    }

    # 删除记录
    protected function _delete ($table, $wheres = '', $sqli = '') {
        $sql = sprintf("DELETE FROM %s %s %s ", $table, $this->_buildWhereClause($wheres), $sqli);
        $stmt = $this->getConnection()->prepare($sql);
        $stmt->execute();
        return intval($stmt->rowCount());
    }

    # 组建插入SQL语句
    protected function _buildInsertClause (array $fields) {
        if (count($fields) < 1) {
            return FALSE;
        }
        $keys = '(';
        $values = ' VALUES (';
        foreach ($fields as $key => $value) {
            $keys .= '`' . $key . '`,';
            $values .= "'" . $this->_checkParam($value) . "',";
        }
        $keys = substr_replace($keys, ')', -1);
        $values = substr_replace($values, ')', -1);
        return $keys . $values;
    }

    # 组建更新SQL语句
    protected function _buildUpdateClause ($fields) {
        $sql = ' SET ';
        foreach ($fields as $key => $value) {
            $sql .= '`' . $key . '`="' . $this->_checkParam($value) . '",';
        }
        return substr_replace($sql, '', -1);
    }

    # 组建查询条件语句
    protected function _buildWhereClause ($wheres) {
        if (!$wheres) {
            return '';
        }
        $sql = ' WHERE ';
        foreach ($wheres as $key => $value) {
            $sql .= ' `' . $key . '` = "' . $this->_checkParam($value) . '" AND';
        }
        return trim($sql, 'AND');
    }

    # 检查数据参数
    protected function _checkParam ($value) {
        return mysql_escape_string($value);
    }

    # 组建LIMIT SQL语句
    protected function _buildLimitSQL ($offset = 1, $limit = 0) {
        return ($limit) ? sprintf(" LIMIT %s,%s", $offset, $limit) : sprintf(" LIMIT %s ", $offset);
    }

    # 组建排序 SQL语句
    protected function _buildOrderBySQL ($field, $flag = true) {
        $order = ($flag) ? 'DESC' : 'ASC';
        return sprintf(" ORDER BY %s %s", $field, $order);
    }

    # 检查整型字符
    protected function checkInt ($value) {
        return (is_int($value)) ? intval($value) : 0;
    }

    # 检查文本字符
    protected function checkString ($value) {
        return (is_string($value)) ? trim($value) : '';
    }

    protected function getException ($e) {
        throw $e;
    }

}
?>